///<reference path='../../typings/tsd.d.ts'/>
import lightwallet = require("eth-lightwallet");
import LogUtil from "../utils/LogUtil";
/**
 * 基础类
 */
export default class Common {


    constructor(){
       
    }

    public static getAddresses(keystore,pwDerivedKey){
        
        if (typeof keystore === "string") {
            keystore = lightwallet.keystore.deserialize(keystore);
        }
        
        keystore.generateNewAddress(pwDerivedKey,1);
        let addresses:string[] = keystore.getAddresses();
        let addr = "0x"+addresses[0];
        
        return addr;
    }

    public static restoreWallet(callback: Function): void {
        var seedPhrase = 'coconut what stem lecture under banana island chicken member walnut decide rocket';
        var password = '123456';
        lightwallet.keystore.createVault({
            password: password,
            seedPhrase: seedPhrase
        }, (err, keystore) => {
            keystore.keyFromPassword(password, (err, pwDerivedKey) => {
                if (err) {
                    throw err;
                }

                let keystoreSerialized = keystore.serialize();//串行化serialize
                keystore.generateNewAddress(pwDerivedKey, 3);
                let addresses = keystore.getAddresses();

                let data = {
                    pwDerivedKey: pwDerivedKey,
                    keystore: keystoreSerialized,
                    addresses: addresses
                };

                callback({'pwDerivedKey':pwDerivedKey,'keystore':keystore,'addresses':addresses});
            });
        });
    }

}